package net.jjjshop.common.ali.sms;


import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AliYunSmsUtil {


    public static String accessKey="LTAI5tMXUQ8vQV2rXPzEmEpc";
    public static String secretKey ="jVqXe500psldp4uPKd9fj3XMKplL1u";
    public static String signName = "领魅时代";

    public static String templateCode ="SMS_471480153";//验证码
    /**
     * 通知模板 SMS_465646050
     * 模板内容
     * 您有新的权益卡订单需要处理，请及时查看
     */
    public static String templateCodeNotice ="SMS_471455146";


    /**
     * 发送短信
     *通用验证码
     * @return
     */
    public static Boolean sendSms(String mobile, Long smsCode) {
        return sendSmsLocal(mobile, smsCode,accessKey, secretKey, signName, templateCode);
    }
    /**
     * 发送短信
     * 自定义模板id
     */
    public static Boolean sendSms(String mobile, Long smsCode,String templateCode) {
        Boolean smsLocal = sendSmsLocal(mobile, smsCode, accessKey, secretKey, signName, templateCode);
        return smsLocal;
    }

    /**
     * 发送短信
     *
     * @return
     */
    public static Boolean sendSmsLocal(String mobile, Long smsCode,String accessKey, String secretKey, String signName, String templateCode) {

        try {
            com.aliyun.dysmsapi20170525.Client client = AliYunSmsUtil.createClient(accessKey, secretKey);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setPhoneNumbers(mobile).setSignName(signName).setTemplateCode(templateCode).setTemplateParam("{\"code\":\"" + smsCode.toString() + "\"}");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("阿里云短信响应结果:{}", JSONUtil.toJsonStr(sendSmsResponse.getBody()));
            String code = sendSmsResponse.getBody().getCode();
            log.info("状态status:{}", code);
            if (!"OK".equals(code)) {
                System.out.println(sendSmsResponse.getBody().getMessage());
                return false;
            }
            log.info("请求ID:{}", sendSmsResponse.getBody().getBizId());
            // return sendSmsResponse.getBody();
            return true;
        } catch (Exception error) {
            // 如有需要，请打印 error
            log.error("错误信息:{}", error.getMessage());
        }
        return false;
    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    private static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }


}
